/**
 * @(#)CodeEntityBodyDependancyRelation.java, 2008-5-2.
 * Copyright 2008  Program Analysis Research Group(PARG),Institute of Software, Peking University.
 * All rights reserved.
 */
package edu.pku.sei.pgie.analyzer.internal.relation;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

import edu.pku.sei.pgie.analyzer.internal.CodeEntitySymbolTable;
import edu.pku.sei.pgie.analyzer.internal.VisitorWithContext;
import edu.pku.sei.pgie.analyzer.util.ASTUtil;

/**
 *
 * @author HeLi
 *
 */
public class CodeEntityBodyDependancyRelation extends
		AbstractCodeEntityRelation {

	public class BodyDependancyVisitor extends VisitorWithContext {
		private List<String[]> container;

		public BodyDependancyVisitor() {
			super(true);
			container = new ArrayList<String[]>();
		}

		public List<String[]> getResult() {
			return container;
		}

		public boolean visit(VariableDeclarationExpression node) {
			Type type = node.getType();
			ITypeBinding typeBinding = type.resolveBinding();
			String componentTypeName = ASTUtil.getComponentTypeName(typeBinding);
			if(componentTypeName != null) {
				container.add(new String[]{peekTypeFQN(),componentTypeName});
			}
			return true;
		}

		public boolean visit(VariableDeclarationStatement node) {
			Type type = node.getType();
			ITypeBinding typeBinding = type.resolveBinding();
			String componentTypeName = ASTUtil.getComponentTypeName(typeBinding);
			if(componentTypeName != null) {
				container.add(new String[]{peekTypeFQN(),componentTypeName});
			}
			return true;
		}
	}

	public String[][] collectCallBack(CompilationUnit root,
			CodeEntitySymbolTable symbolTable) throws Exception {
		BodyDependancyVisitor visitor = new BodyDependancyVisitor();
		root.accept(visitor);
		List<String[]> result = visitor.getResult();
		return result.toArray(new String[result.size()][]);
	}

}
